package controller

import (
	"io"
	"os"

	"github.com/gin-gonic/gin"
)

func Down(c *gin.Context) {
	filePath := c.Query("file")

	file, err := os.Open(filePath)
	if err != nil {
		Fail(c, "打开文件失败")
		return
	}
	defer file.Close()

	// 设置响应头，告诉浏览器这是一个下载文件
	c.Header("Content-Disposition", "attachment; filename=file.txt")
	c.Header("Content-Type", "application/octet-stream")

	// 将文件内容写入到响应体中
	_, err = io.Copy(c.Writer, file)
	if err != nil {
		Fail(c, "写入下载文件失败")
		return
	}

}
